/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_CPP_RTPS_TRANSPORT_UDSSTREAMACCEPTOR_H_
#define SRC_CPP_RTPS_TRANSPORT_UDSSTREAMACCEPTOR_H_

#include <asio.hpp>
#include <ertps/utils/LocatorAlias.h>
#include <deps/common/IPLocator.h>

using IPLocator = vbsutil::xmlparser::IPLocator;
using Locator = evbs::edds::rtps::Locator;

namespace vbs {
namespace transport {

class UDSStreamTransportInterface;

class UDSStreamAcceptor {
 public:
    UDSStreamAcceptor(asio::io_service& io_service, UDSStreamTransportInterface* parent, const Locator& locator);

    UDSStreamAcceptor(asio::io_service& io_service, const std::string& interface, const Locator& locator);

    UDSStreamAcceptor(asio::io_service& io_service, const Locator& locator);  //locator.address为被监听path

    const Locator& locator() const { return locator_; }

    Locator& locator() { return locator_; }

    ~UDSStreamAcceptor() {
        cancel();
        close();
        if (std::remove(IPLocator::getPathFromLocator(locator_).c_str()) != 0) {
            logInfo(UDSSTREAM, "Failed to delete the UDSStream Acceptor!!!");
        }
    }

    void accept(UDSStreamTransportInterface* parent);

    void cancel() { acceptor_.cancel(); }

    void close() { acceptor_.close(); }

 private:
    asio::io_service* io_service_;
    Locator locator_;
    asio::local::stream_protocol::socket socket_;  // 感觉这个socket没有用
    asio::local::stream_protocol::acceptor acceptor_;
};

}  // namespace transport
}  // namespace vbs

#endif
